import { MigrationInterface, QueryRunner } from "typeorm";

export class CreatePermission implements MigrationInterface {
    name = 'CreatePermission.ts1726822592986'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`permission\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(50) NOT NULL, \`desc\` varchar(100) NULL, \`createTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateTime\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`user_permission_relation\` (\`loginUserId\` int NOT NULL, \`permissionId\` int NOT NULL, INDEX \`IDX_aab75b70906b2331d12c607555\` (\`loginUserId\`), INDEX \`IDX_ed47405f30131c99ca1f398c50\` (\`permissionId\`), PRIMARY KEY (\`loginUserId\`, \`permissionId\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`user_permission_relation\` ADD CONSTRAINT \`FK_aab75b70906b2331d12c6075551\` FOREIGN KEY (\`loginUserId\`) REFERENCES \`login_user\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`);
        await queryRunner.query(`ALTER TABLE \`user_permission_relation\` ADD CONSTRAINT \`FK_ed47405f30131c99ca1f398c508\` FOREIGN KEY (\`permissionId\`) REFERENCES \`permission\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`user_permission_relation\` DROP FOREIGN KEY \`FK_ed47405f30131c99ca1f398c508\``);
        await queryRunner.query(`ALTER TABLE \`user_permission_relation\` DROP FOREIGN KEY \`FK_aab75b70906b2331d12c6075551\``);
        await queryRunner.query(`DROP INDEX \`IDX_ed47405f30131c99ca1f398c50\` ON \`user_permission_relation\``);
        await queryRunner.query(`DROP INDEX \`IDX_aab75b70906b2331d12c607555\` ON \`user_permission_relation\``);
        await queryRunner.query(`DROP TABLE \`user_permission_relation\``);
        await queryRunner.query(`DROP TABLE \`permission\``);
    }

}
